﻿///***************************************************************
/// 시  스  템 : Updms.Mny
/// 단위시스템 : MNY
/// 계      층 : Presentation Layer
/// 프로그램명 : Mny2018r
/// 파  일  명 : Mny2018r.aspx.cs
/// 설      명 : 예산대비지출현황
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-12-16 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Adapter;
using SMFramework.Data;
using SMFramework.Security;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.SMGridView;

using Base.AppFramework.Web.UI.CommandBar;
using Base.Dac;

using Updms.Common.BaseClass;
using Updms.Mny.Exe;


namespace Updms.Mny
{
	public partial class Mny2018r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Mny2018r";
		public static readonly string gs_path_info = "/updms/Mny/" + PID + ".aspx";

		Global global = new Global();
		Crypto crypto = new Crypto();

		//검색
		protected string kw1 = "";	// 기준년월

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		//합계
		decimal ld_budget = 0;
		decimal ld_out_amount = 0;
		decimal ld_balance_amount = 0;

		//계정별상세리스트 합계
		decimal ld_detail_total = 0;

		//계정별상세리스트(전월) 합계
		bool lb_checked = false;
		decimal ld_pre_month_detail_total = 0;

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SearchProcess();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetBaseYear();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetClientScript

		private void SetClientScript()
		{
			//onKeyUp이벤트
			//txt_XXX.Attributes.Add("onkeyup", "document.form1.txt_limit_use.value=addComma(document.form1.txt_txt_XXX.value);");
		}

		#endregion

		#region SetInit, SetSearchParameter

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();

			gs_page_mode = (Request.QueryString["page_mode"] == null || Request.QueryString["page_mode"].Trim().Length == 0)
					? "" : Request.QueryString["page_mode"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						ddl_search_base_year.SelectedValue = kw1.Substring(0, 4).ToString();
						ddl_search_base_month.SelectedValue = kw1.Substring(5, 2).ToString();
					}
					break;
				case "SEARCH":
					kw1 = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
					break;
			}
		}


		#endregion

		#region 마스터 Method (SetBaseYear, CreateFileReal)

		//기준년도 설정
		private void SetBaseYear()
		{
			ddl_search_base_year.Items.Clear();

			int li_f_year = Convert.ToInt32(SEARCH_Y_MIN);
			int li_t_year = Convert.ToInt32(DateUtil.GetYearEx(1, "yyyy"));
			for (int i = li_f_year; i <= li_t_year; i++)
			{
				string ls_year = i.ToString();
				ddl_search_base_year.Items.Add(new ListItem(ls_year, ls_year));
			}

			ddl_search_base_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy"); // 기본값은 현재년월
			ddl_search_base_month.SelectedValue = DateUtil.GetMonthEx(0, "MM");
		}

		//실제파일생성
		private void CreateFileReal(string ls_file_nm, DataSet ds_in, DataSet ds_budget)
		{
			try
			{
				string baseYm = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
				string tableName = "UPDMS_MNY_REPORT";
				string ls_stream = FILE_PATH_FTP_UPLOAD + "\\" + ls_file_nm;
				FileInfo file = new FileInfo(ls_stream);

				StreamWriter sw = file.CreateText();

				sw.WriteLine(CommUtil.GetDefaultHtmlTag(PID, global.GetMenuTitle(PID, LANG_SET) + "(" + baseYm + ")"));

				//수입
				sw.WriteLine("<table class=\"report\">");
				sw.WriteLine("<tr>");
				sw.WriteLine("<th>구분</th>");
				sw.WriteLine("<th>등록일</th>");
				sw.WriteLine("<th>계정</th>");
				sw.WriteLine("<th>내용</th>");
				sw.WriteLine("<th>금액</th>");
				sw.WriteLine("<th>비고</th>");
				sw.WriteLine("<th>합계</th>");
				sw.WriteLine("</tr>");

				int rCountIn = DataSetUtil.GetRowCount(ds_in, tableName);
				for (int i = 0; i < rCountIn; i++)
				{
					string regDt = DataSetUtil.GetFldStr(ds_in, tableName, i, "Reg_Dt");
					string accountNm = DataSetUtil.GetFldStr(ds_in, tableName, i, "Account_Nm");
					string contents = DataSetUtil.GetFldStr(ds_in, tableName, i, "Contents");
					string amount = ConversionUtil.AddComma(DataSetUtil.GetFldStr(ds_in, tableName, i, "Amount"));
					string remark = DataSetUtil.GetFldStr(ds_in, tableName, i, "Remark");
					string totalAmt = ConversionUtil.AddComma(DataSetUtil.GetFldStr(ds_in, tableName, i, "Total_Amt"));

					sw.WriteLine("<tr>");
					//구분 rowspan
					if (i == 0)
					{
						sw.WriteLine("<td class=\"lbl_c\" rowspan=\"" + rCountIn + "\">수입</td>");
					}
					sw.WriteLine("<td class=\"lbl_c\">" + regDt + "</td>");
					sw.WriteLine("<td class=\"lbl\">" + accountNm + "</td>");
					sw.WriteLine("<td class=\"lbl\">" + contents + "</td>");
					sw.WriteLine("<td class=\"data\">" + amount + "</td>");
					sw.WriteLine("<td class=\"lbl\">" + remark + "</td>");

					if (i == 0)
					{
						sw.WriteLine("<td class=\"lbl_c b\" rowspan=\"" + rCountIn + "\">" + totalAmt + "</td>");
					}
					sw.WriteLine("</tr>");
				}
				sw.WriteLine("</table>");

				sw.WriteLine("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">");
				sw.WriteLine("<tr>");
				sw.WriteLine("<td>");
				//예산대비지출
				sw.WriteLine("<table id=\"report_table\" class=\"report\">");
				sw.WriteLine("<tr>");
				sw.WriteLine("<th>구분</th>");
				sw.WriteLine("<th>계정</th>");
				sw.WriteLine("<th>예산</th>");
				sw.WriteLine("<th>나간돈</th>");
				sw.WriteLine("<th>남은돈</th>");
				sw.WriteLine("</tr>");

				int rCountBudget = DataSetUtil.GetRowCount(ds_budget, tableName);
				int tBudget = 0;	//합계
				int tOutAmount = 0;	//합계
				int tBalanceAmount = 0;	//합계

				for (int i = 0; i < rCountBudget; i++)
				{
					string classNm = DataSetUtil.GetFldStr(ds_budget, tableName, i, "Class_Nm");
					string accountNm = DataSetUtil.GetFldStr(ds_budget, tableName, i, "Account_Nm");

					int iBudget = DataSetUtil.GetFldInt(ds_budget, tableName, i, "Budget");
					int iOutAmount = DataSetUtil.GetFldInt(ds_budget, tableName, i, "Out_Amount");
					int iBalanceAmount = DataSetUtil.GetFldInt(ds_budget, tableName, i, "Balance_Amount");
					
					tBudget += iBudget;
					tOutAmount += iOutAmount;
					tBalanceAmount += iBalanceAmount;

					string budget = ConversionUtil.AddComma(iBudget.ToString());
					string outAmount = ConversionUtil.AddComma(iOutAmount.ToString());
					string balanceAmount = ConversionUtil.AddComma(iBalanceAmount.ToString());

					sw.WriteLine("<tr>");
					sw.WriteLine("<td class=\"lbl_c\">" + classNm + "</td>");
					sw.WriteLine("<td class=\"lbl\">" + accountNm + "</td>");
					sw.WriteLine("<td class=\"data\">" + budget + "</td>");
					sw.WriteLine("<td class=\"data\">" + outAmount + "</td>");
					sw.WriteLine("<td class=\"data\">" + balanceAmount + "</td>");
					sw.WriteLine("</tr>");
				}
				sw.WriteLine("<tr class=\"total\">");
				sw.WriteLine("<td class=\"lbl_c\" colspan=\"2\">Total</td>");
				sw.WriteLine("<td class=\"data\">" + ConversionUtil.AddComma(tBudget.ToString()) + "</td>");
				sw.WriteLine("<td class=\"data\">" + ConversionUtil.AddComma(tOutAmount.ToString()) + "</td>");
				sw.WriteLine("<td class=\"data\">" + ConversionUtil.AddComma(tBalanceAmount.ToString()) + "</td>");
				sw.WriteLine("</table>");
				sw.WriteLine("</td>");
				sw.WriteLine("<td valign=\"top\">");
				sw.WriteLine("<table class=\"report\">");
				sw.WriteLine("<tr>");
				sw.WriteLine("<td style=\"padding:28px 0 0 0;\">");
				sw.WriteLine("<img src=\"" + PID + ".png" + "\" width=\"471\" height=\"1021\" />");
				sw.WriteLine("</td>");
				sw.WriteLine("</tr>");
				sw.WriteLine("</table>");
				sw.WriteLine("</tr>");
				sw.WriteLine("</table>");
				sw.WriteLine("<script type=\"text/javascript\" language=\"javascript\" defer=\"defer\">");
				sw.WriteLine("TableRowspan(report_table, '0');");
				sw.WriteLine("</script>");
				sw.WriteLine("</body>");
				sw.WriteLine("</html>");

				sw.Close();

				//챠트 Export
				chart_budget_radial.Export(ChartFX.WebForms.FileFormat.Png, FILE_PATH_FTP_UPLOAD + "\\" + PID +".png");
			}
			catch (Exception e)
			{
				Base.AppFramework.SMEventLog.WriteExceptionLog(e);
				CallMsgBoxInAjax("MSG_ERR_122");	//파일을 생성하지 못했습니다
			}
		}

		#endregion

		#region 마스터 Method (SetPrevNextMonth, SetEnabledMonthBtn, SetThisMonth)

		//이전달, 다음달버튼 누르면
		private void SetPrevNextMonth(int pi_month_cnt)
		{
			string ls_ym = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
			string ls_calc_ym = DateUtil.GetCalcMonth(ls_ym, pi_month_cnt, "yyyy-MM");

			if (WebControlUtil.CheckDdlValue(ddl_search_base_year, ls_calc_ym.Substring(0, 4).ToString()))
			{
				ddl_search_base_year.SelectedValue = ls_calc_ym.Substring(0, 4).ToString();
				ddl_search_base_month.SelectedValue = ls_calc_ym.Substring(5, 2).ToString();
			}

			//버튼처리
			SetEnabledMonthBtn();
		}

		//이전달, 다음달버튼 처리
		private void SetEnabledMonthBtn()
		{
			ibtn_prev_month.Enabled
				= (ddl_search_base_year.SelectedIndex == 0 && ddl_search_base_month.SelectedIndex == 0) ? false : true;

			ibtn_next_month.Enabled
				= (ddl_search_base_year.SelectedIndex == ddl_search_base_year.Items.Count - 1 && ddl_search_base_month.SelectedIndex == 11) ? false : true;
		}

		//당월설정
		private void SetThisMonth()
		{
			string ls_this_ym = DateUtil.GetNow("yyyy-MM");
			ddl_search_base_year.SelectedValue = ls_this_ym.Substring(0, 4).ToString();
			ddl_search_base_month.SelectedValue = ls_this_ym.Substring(5, 2).ToString();
			SetEnabledMonthBtn();
		}

		#endregion

		#region 챠트 (DrawBudgetRadial)

		//예산대비지출챠트
		private void DrawBudgetRadial(DataSet lo_ds)
		{
			//초기화
			chart_budget_radial.Data.Clear();

			//시리즈 갯수
			chart_budget_radial.Data.Series = 2;

			int li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_MNY_REPORT");

			//Chart높이 (1행 = 22px)
			chart_budget_radial.Height = 22 * li_count + 32;

			for (int i = 0; i < li_count; i++)
			{
				//데이터바인딩 (밑에서부터 바인딩)
				chart_budget_radial.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", (li_count - i) - 1, "Out_Amount");
				chart_budget_radial.Data[1, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", (li_count - i) - 1, "Balance_Amount");

				//X축 라벨링
				chart_budget_radial.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, "UPDMS_MNY_REPORT", (li_count - i) - 1, "Account_Nm");
			}
		}

		//예산초과계정분포챠트
		private void DrawOverBudgetPie(DataSet lo_ds)
		{
			//초기화
			chart_over_budget.Data.Clear();

			//시리즈 갯수
			chart_over_budget.Data.Series = 1;

			int li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_MNY_REPORT");

			for (int i = 0; i < li_count; i++)
			{
				//남은돈이 마이너스만 바인딩
				if (DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "Balance_Amount") < 0)
				{
					//데이터바인딩
					chart_over_budget.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "Balance_Amount");

					//X축 라벨링
					chart_over_budget.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, "UPDMS_MNY_REPORT", i, "Account_Nm");
				}
			}
		}

		#endregion

		#region 리스트 (SetInAmountList, SetOutAmountListByBudget, SetOutAmountListByAccount, SearchProcess)

		//수입상세리스트
		private void SetInAmountList()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_base_ym", typeof(string), kw1);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Mny2018r_NT _Mny2018r_NT = new Mny2018r_NT())
			{
				lo_ds.Merge(_Mny2018r_NT.GetInAmountList(lo_dp));
			}

			gv_income.DataSource = lo_ds;
			gv_income.DataBind();
		}

		//예산대비지출현황
		private void SetOutAmountListByBudget(DataSet lo_ds)
		{
			int li_max_over = 0;
			string ls_account = "AC001";	//식비(기본값)
			for (int i = 0; i < DataSetUtil.GetRowCount(lo_ds, "UPDMS_MNY_REPORT"); i++)
			{
				//남은돈이 마이너스인것만 조회
				if (DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "Balance_Amount") < 0)
				{
					if (li_max_over > DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "Balance_Amount"))
					{
						li_max_over = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", i, "Balance_Amount");
						ls_account = DataSetUtil.GetFldStr(lo_ds, "UPDMS_MNY_REPORT", i, "Account");
					}
				}
			}

			gv_out_by_budget.DataSource = lo_ds;
			gv_out_by_budget.DataBind();
			
			//가장 많이 초과된 계정을 먼저 보여준다.
			SetOutAmountListByAccount(ls_account);
			SetPreMonthOutAmountListByAccount(ls_account);
		}

		//계정별상세리스트
		private void SetOutAmountListByAccount(string ls_account)
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_base_ym", typeof(string), kw1);
			lo_dp.AddProperty("ls_account", typeof(string), ls_account);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);
			
			using (Mny2018r_NT _Mny2018r_NT = new Mny2018r_NT())
			{
				lo_ds.Merge(_Mny2018r_NT.GetOutAmountListByAccount(lo_dp));
			}

			lbl_account_nm.Text = ""; //라벨 초기화
			if (DataSetUtil.CheckRowCount(lo_ds, "UPDMS_MNY_REPORT"))
			{
				string ls_account_nm = DataSetUtil.GetFldStr(lo_ds, "UPDMS_MNY_REPORT", "Account_Nm");
				lbl_account_nm.Text = ls_account_nm;
			}

			gv_amount_list_by_account.DataSource = lo_ds;
			gv_amount_list_by_account.DataBind();
		}

		//계정별상세리스트(전월)
		private void SetPreMonthOutAmountListByAccount(string ls_account)
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_base_ym", typeof(string), DateUtil.GetCalcMonth(kw1, -1, "yyyy-MM"));
			lo_dp.AddProperty("ls_account", typeof(string), ls_account);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Mny2018r_NT _Mny2018r_NT = new Mny2018r_NT())
			{
				lo_ds.Merge(_Mny2018r_NT.GetOutAmountListByAccount(lo_dp));
			}

			lbl_pre_month_account_nm.Text = ""; //라벨 초기화
			if (DataSetUtil.CheckRowCount(lo_ds, "UPDMS_MNY_REPORT"))
			{
				string ls_account_nm = DataSetUtil.GetFldStr(lo_ds, "UPDMS_MNY_REPORT", "Account_Nm");
				lbl_pre_month_account_nm.Text = ls_account_nm;
			}

			gv_pre_month_amount_list_by_account.DataSource = lo_ds;
			gv_pre_month_amount_list_by_account.DataBind();
		}

		//검색
		private void SearchProcess()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();
			
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			//검색
			lo_dp.AddProperty("ls_base_ym", typeof(string), kw1);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Mny2018r_NT _Mny2018r_NT = new Mny2018r_NT())
			{
				lo_ds.Merge(_Mny2018r_NT.GetOutAmountListByBudget(lo_dp));
			}
			
			SetInAmountList();
			SetOutAmountListByBudget(lo_ds);
			DrawBudgetRadial(lo_ds);
			DrawOverBudgetPie(lo_ds);
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
					//사용권한별 버튼처리
					PagePreRender.SetVisibleCtrl(this, access.CheckPgmUsingGrant(USER_DIV, PID), access.GetVisibleCtrl(PID));
				}
			}
			ChangePageMode();
		}

		//ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":		//처음 페이지로드시
					Top.Visible = true;
					menu_area.Attributes.Add("class", "");
					ibtn_close.Visible = false;
					break;
				case "P":		//팝업으로 열때
					Top.Visible = false;
					menu_area.Attributes.Add("class", "nodisplay");
					ibtn_close.Visible = true;
					break;
			}
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		//예산지출현황
		protected void gv_out_by_budget_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			GridView gv = (GridView)sender;
			
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
				e.Row.Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);

				//마이너스는 빨강색
				if (e.Row.Cells[6].Text.Substring(0, 1) == "-")
				{
					e.Row.Cells[6].Attributes.Add("class", "color_red");
				}

				//상세
				string ls_account = gv.DataKeys[e.Row.RowIndex].Values["Account"].ToString();
				e.Row.Cells[7].Text = "<div class=\"sbtn sbtn_report\" title=\"상세\" onclick=\"viewDetail('" + ls_account + "');\" />";
			}

			//합계
			ld_budget += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Budget"));
			ld_out_amount += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Out_Amount"));
			ld_balance_amount += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Balance_Amount"));			

			//Footer에 합계 바인딩
			if (e.Row.RowType == DataControlRowType.Footer)
			{
				e.Row.Cells[0].Visible = false;	//0.Class
				e.Row.Cells[1].Text = "Total";	//1.Class_Nm
				e.Row.Cells[1].Attributes.Add("class", "total");
				e.Row.Cells[1].ColumnSpan = 2;
				e.Row.Cells[2].Visible = false;	//2.Account
				e.Row.Cells[3].Visible = false;	//3.Account_Nm
				e.Row.Cells[4].Text = ld_budget.ToString("#,###,##0");
				e.Row.Cells[4].Attributes.Add("class", "money");
				e.Row.Cells[5].Text = ld_out_amount.ToString("#,###,##0");
				e.Row.Cells[5].Attributes.Add("class", "money");
				e.Row.Cells[6].Text = ld_balance_amount.ToString("#,###,##0");
				e.Row.Cells[6].Attributes.Add("class", "money");
				e.Row.Cells[7].Text = "";
				e.Row.Cells[7].Attributes.Add("class", "none");
			}

			//머지
			SMGridView sGv = new SMGridView(this.Page, ref gv_out_by_budget);
			sGv.RowSpanGridView(gv_out_by_budget, 1);	//1.Class_Nm
		}

		//계정별상세리스트
		protected void gv_amount_list_by_account_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
				e.Row.Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);
			}

			//합계
			ld_detail_total += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Amount"));
			
			//Footer에 합계 바인딩
			if (e.Row.RowType == DataControlRowType.Footer)
			{
				e.Row.Cells[0].Text = "Total";
				e.Row.Cells[0].Attributes.Add("class", "total");
				e.Row.Cells[0].ColumnSpan = 5;
				e.Row.Cells[1].Visible = false;
				e.Row.Cells[2].Visible = false;
				e.Row.Cells[3].Visible = false;
				e.Row.Cells[4].Visible = false;
				e.Row.Cells[5].Text = ld_detail_total.ToString("#,###,##0");
				e.Row.Cells[5].Attributes.Add("class", "money");
			}

			//머지
			SMGridView sGv = new SMGridView(this.Page, ref gv_amount_list_by_account);
			sGv.RowSpanGridView(gv_amount_list_by_account, 0);
			sGv.RowSpanGridView(gv_amount_list_by_account, 1);
			sGv.RowSpanGridView(gv_amount_list_by_account, 2);
		}

		//수입리스트
		protected void gv_income_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			//머지
			SMGridView sGv = new SMGridView(this.Page, ref gv_income);
			sGv.RowSpanGridView(gv_income, 1);
			sGv.RowSpanGridView(gv_income, 5);
		}

		//계정별상세리스트(전월)
		protected void gv_pre_month_amount_list_by_account_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
				e.Row.Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);
			}

			//합계
			ld_pre_month_detail_total += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Amount"));

			//Footer에 합계 바인딩
			if (e.Row.RowType == DataControlRowType.Footer)
			{
				e.Row.Cells[0].Text = "Total";
				e.Row.Cells[0].Attributes.Add("class", "total");
				e.Row.Cells[0].ColumnSpan = 6;
				e.Row.Cells[1].Visible = false;
				e.Row.Cells[2].Visible = false;
				e.Row.Cells[3].Visible = false;
				e.Row.Cells[4].Visible = false;
				e.Row.Cells[5].Visible = false;
				e.Row.Cells[6].Text = ld_pre_month_detail_total.ToString("#,###,##0");
				e.Row.Cells[6].Attributes.Add("class", "money");
			}

			//머지
			SMGridView sGv = new SMGridView(this.Page, ref gv_pre_month_amount_list_by_account);
			sGv.RowSpanGridView(gv_pre_month_amount_list_by_account, 1);
			sGv.RowSpanGridView(gv_pre_month_amount_list_by_account, 2);
			sGv.RowSpanGridView(gv_pre_month_amount_list_by_account, 3);
		}

		//체크박스선택시
		protected void cb_select_CheckedChanged(object sender, EventArgs e)
		{
			decimal ld_checked_amount = 0;
			decimal ld_total_amount = 0;

			//선택한 Row의 금액,비고를 활성화 한다.
			for (int i = 0; i < gv_pre_month_amount_list_by_account.Rows.Count; i++)
			{
				CheckBox cb_select = (CheckBox)gv_pre_month_amount_list_by_account.Rows[i].FindControl("cb_select");

				if (cb_select.Checked)
				{
					//선택된금액
					ld_checked_amount += Convert.ToDecimal(ConversionUtil.RemoveComma(gv_pre_month_amount_list_by_account.Rows[i].Cells[6].Text));
					lb_checked = true;
				}

				//전체합계금액
				ld_total_amount += Convert.ToDecimal(ConversionUtil.RemoveComma(gv_pre_month_amount_list_by_account.Rows[i].Cells[6].Text));
			}

			//체크되어 있으면 선택된 금액만
			if (lb_checked)
			{
				gv_pre_month_amount_list_by_account.FooterRow.Cells[6].Text = ld_checked_amount.ToString("#,###,##0");
			}
			else
			{
				//그렇지 않으면 전체합계금액을
				gv_pre_month_amount_list_by_account.FooterRow.Cells[6].Text = ld_total_amount.ToString("#,###,##0");
			}
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SetEnabledMonthBtn();
			SearchProcess();
		}

		//엑셀
		protected void ibtn_excel_Click(object sender, EventArgs e)
		{
			SetSearchParameter();

			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_base_ym", typeof(string), kw1);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Mny2018r_NT _Mny2018r_NT = new Mny2018r_NT())
			{
				lo_ds.Merge(_Mny2018r_NT.GetOutAmountListByBudget(lo_dp));
			}
			string fileName = PID + "(" + global.GetMenuTitle(PID, LANG_SET) + ")";
			ExcelUtil.CreateExcelFileX(this.Page, fileName, lo_ds, SetExcelHeaderX(global.GetColumnListByPgmId(PID, LANG_SET)));
		}

		//리스트에서 상세 클릭
		protected void lbtn_view_detail_Click(object sender, EventArgs e)
		{
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();
			SetOutAmountListByAccount(h_account.Value);
			SetPreMonthOutAmountListByAccount(h_account.Value);
		}

		//FTP
		protected void ibtn_ftp_Click(object sender, EventArgs e)
		{
			string fileName = PID + ".html";
			string chartFileName = PID + ".png";
			FtpClient fc = new FtpClient(FTP_HOSTNAME, FTP_USERID, crypto.Decrypt(FTP_PASSWORD));

			try
			{
				SetSearchParameter();
			
				DataSet lo_ds_in = new DataSet();
				DataSet lo_ds_budget = new DataSet();
				DataPack lo_dp = new DataPack();

				lo_dp.AddProperty("ls_base_ym", typeof(string), kw1);
				lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

				using (Mny2018r_NT _Mny2018r_NT = new Mny2018r_NT())
				{
					lo_ds_in.Merge(_Mny2018r_NT.GetInAmountList(lo_dp));
					lo_ds_budget.Merge(_Mny2018r_NT.GetOutAmountListByBudget(lo_dp));
				}

				//파일생성
				CreateFileReal(fileName, lo_ds_in, lo_ds_budget);

				//FTP전송
				if (FileUtil.IsFileExist(FILE_PATH_FTP_UPLOAD + "\\" + fileName))
				{
					fc.Login();
					fc.BinaryMode = false;

					string[] remoteDir = StringUtil.DelimitedListToStringArray(FTP_DIR_IPHONE_MNY, "/");
					for (int i = 0; i < remoteDir.Length; i++)
					{
						fc.ChangeDir(remoteDir[i]);
					}

					if (fc.IsFileExist(fileName))
					{
						fc.DeleteFile(fileName);
					}

					if (fc.IsFileExist(chartFileName))
					{
						fc.DeleteFile(chartFileName);
					}

					fc.UploadOnlyFile(FILE_PATH_FTP_UPLOAD, fileName, "", "", true);
					fc.UploadOnlyFile(FILE_PATH_FTP_UPLOAD, chartFileName, "", "", true);

					fc.Close();
				}
				else
				{
					CallMsgBoxInAjax("MSG_ERR_164");	//전송할 파일이 없습니다.
				}
				CallMsgBoxInAjax("MSG_INF_018");	//전송되었습니다
			}
			catch (Exception ex)
			{
				CallExceptionMsgBoxInAjax(ex);
			}
			finally
			{
				fc.Close();
			}
		}

		//이전달
		protected void ibtn_prev_month_Click(object sender, EventArgs e)
		{
			SetPrevNextMonth(-1);
			SearchProcess();
		}

		//다음달
		protected void ibtn_next_month_Click(object sender, EventArgs e)
		{
			SetPrevNextMonth(1);
			SearchProcess();
		}

		//당월
		protected void ibtn_this_month_Click(object sender, EventArgs e)
		{
			SetThisMonth();
			SearchProcess();
		}

		#endregion

	}
}
